﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Equip
{
    public Equip(int id, int number)
    {
        this.id = id;
        this.number = number;
    }
    public int id;
    public int number;
}
public class Player
{
    public int id;
    public string name;
    public int age;
    public int att;
    public int def;
    public List<Equip> equips  = new List<Equip>();

    public Player(int id, string name)
    {
        this.id = id;
        this.name = name;
    }
    public void Load()
    {
        name = PlayerPrefs.GetString(GetKey("name"), "");
        age = PlayerPrefs.GetInt(GetKey("age"), 18);
        att = PlayerPrefs.GetInt(GetKey("att"), 100);
        def = PlayerPrefs.GetInt(GetKey("def"), 50);
        Debug.LogFormat("Load name:{0} age:{1} att:{2} def:{3}", name, age, att, def);

        int itemNum = PlayerPrefs.GetInt(GetKey("itemNum"), 0);
        for (int i = 0; i < itemNum; i++)
        {
            int equip_id = PlayerPrefs.GetInt(GetKey("itemID" + i));
            int equip_num = PlayerPrefs.GetInt(GetKey("itemNum" + i));
            equips.Add(new Equip(equip_id, equip_num));
            Debug.LogFormat("[Player][Load] Equip equip_id:{0} equip_num:{1}", equip_id, equip_num);
        }
    }

    public string GetKey(string key)
    {
        return this.id + key;
    }
    public void AddEquip(int id, int number)
    {
        equips.Add(new Equip(id, number));
    }
    public void Save()
    {
        PlayerPrefs.SetString(GetKey("name"), name);
        PlayerPrefs.SetInt(GetKey("age"), age);
        PlayerPrefs.SetInt(GetKey("att"), att);
        PlayerPrefs.SetInt(GetKey("def"), def);

        PlayerPrefs.SetInt(GetKey("itemNum"), equips.Count);
        for (int i=0; i< equips.Count; i++)
        {
            PlayerPrefs.SetInt(GetKey("itemID" + i), equips[i].id);
            PlayerPrefs.SetInt(GetKey("itemNum" + i), equips[i].number);
        }
        PlayerPrefs.Save();
    }
}

public class Rank
{
    public string name;
    public int score;
    public int time;
    public Rank(string name, int score, int time)
    {
        this.name = name;
        this.score = score;
        this.time = time;
    }
}
public class RankData
{
    public List<Rank> ranks = new List<Rank>();
    public string GetKey(string name)
    {
        return "rank" + name;
    }
    public void Add(string name, int score, int time)
    {
        ranks.Add(new Rank(name, score, time));
    }

    public void Load()
    {
        int rankNum = PlayerPrefs.GetInt(GetKey("num"), 0);
        for (int i=0; i<rankNum; i++)
        {
            string name = PlayerPrefs.GetString(GetKey("name") + i);
            int score = PlayerPrefs.GetInt(GetKey("score") + i);
            int time = PlayerPrefs.GetInt(GetKey("time") + i);
            this.Add(name, score, time);
            Debug.LogFormat("[RankData][Load] name:{0} score:{1} time:{2}", name, score, time);
        }
    }
    public void Save()
    {
        PlayerPrefs.SetInt(GetKey("num"), ranks.Count);
        for (int i=0; i<ranks.Count; i++)
        {
            PlayerPrefs.SetString(GetKey("name")+i, ranks[i].name);
            PlayerPrefs.SetInt(GetKey("score") + i, ranks[i].score);
            PlayerPrefs.SetInt(GetKey("time") + i, ranks[i].time);
        }
    }
}
public class Leasson01_CaseCode : MonoBehaviour
{
    void Start()
    {
        #region 练习题
        //现在有玩家信息类，有名字，年龄，攻击力，防御力等成员现在为其封装两个方法，一个用来存储数据，一个用来读取数据
        //上一题的玩家类中包含一个List存储了拥有的所有装备信息。请在上一题的基础上，把装备信息的存储和读取加上
        Player p1 = new Player(10000, "小明");
        p1.age = 34;
        p1.att = 1000;
        p1.def = 500;
        p1.AddEquip(100, 10);
        p1.AddEquip(101, 9);
        p1.Save();
        p1.Load();

        Player p2 = new Player(10001, "小红");
        p2.age = 25;
        p2.att = 2000;
        p2.def = 1000;
        p2.AddEquip(200, 10);
        p2.AddEquip(201, 9);
        p2.Save();
        p2.Load();

        RankData rankData = new RankData();
        rankData.Add("小明", 100, 60);
        rankData.Add("小红", 200,30);
        rankData.Add("小赵", 500, 80);
        rankData.Save();
        rankData.Load();
        #endregion
    }
}
